package com.maotouying.owlsmart.moviePage;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.maotouying.owlsmart.R;

import com.maotouying.owlsmart.SmartUtil.NoScrollGridView;
import com.maotouying.owlsmart.SmartUtil.SystemUtil;
import com.maotouying.owlsmart.moviePage.List.list_A;
import com.maotouying.owlsmart.moviePage.List.list_C;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class movieListActivity extends AppCompatActivity {
    private ImageView iv_back;
    private list_C list_c;
private list_A list_a;
    private List<list_C> list_cs= new ArrayList<list_C>();
private NoScrollGridView lv_movielist;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie_list);

        //顶部框颜色
        SystemUtil.setStatusBarColor(this, this.getResources().getColor(R.color.top_background_color));

        //视图
        lv_movielist =(NoScrollGridView)findViewById(R.id.lv_movielist);
        //返回按钮
        iv_back = (ImageView)findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //http://103.45.145.67/testfolder/search.php?content=普罗米
        //1。拿到返回的参数
        Intent intent=getIntent();
        String movieSearch=intent.getStringExtra("search");

        //2。去请求接口
        okhttp_text(movieSearch);
    }




    public void okhttp_text(String search) {

        //清空一下，防止重复加载
        list_cs.clear();

        //创建okHttpclient的对象
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建Request,Builder
        Request.Builder builder = new Request.Builder();
        //使用Request.Builder对象,调用Url方法,传入网路路径
        Request.Builder url = builder.url("http://103.45.145.67/testfolder/msearch.php?content="+search);
        //使用Request.Builder对象,调用builder方法构件request对象
        Request request = url.build();
        //创建一个Call对象,参数就request对象
        Call call = okHttpClient.newCall(request);
        //使用call对象,调用enqueue方法,请求加入调度(异步加载)

        call.enqueue(new Callback() {
            //当请求失败的时候,调用此方法
            @Override
            public void onFailure(Call call, IOException e) {
                //andler.sendEmptyMessage(FALL);
            }

            //当请求成功的时候,调用此方法
            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String source = response.body().string();
                System.out.println(source);

               // vod_id,vod_name,vod_class,vod_year,vod_pic
                //开始循环遍历以上字段
//list_c

                List<list_C> ls = JSON.parseArray(source, list_C.class);
                for (list_C pp : ls) {
                    list_c = new list_C();
                    String vod_id = pp.getVod_id();
                    String vod_name = pp.getVod_name();
                    String vod_class = pp.getVod_class();
                    String vod_year = pp.getVod_year();
                    String vod_pic = pp.getVod_pic();

                    list_c.setVod_id(vod_id);
                    list_c.setVod_name(vod_name);
                    list_c.setVod_class(vod_class);
                    list_c.setVod_year(vod_year);
                    list_c.setVod_pic(vod_pic);

                    list_cs.add(list_c);

                }
                Message msg = new Message();
                msg.what = 21;
                Thandler.sendMessage(msg);//用activity中的handler发送消息


            }
        });

    }

    private Handler Thandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 21:
                    clickData((ArrayList<list_C>) list_cs);
                    break;

            }
        }
    };

    public void clickData(ArrayList<list_C> list_cs) {

        list_a = new list_A(R.layout.item_movie, movieListActivity.this, list_cs);


        lv_movielist.setAdapter(list_a);
        lv_movielist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // Toast.makeText(ChartActivity.this, "Click item" + i, Toast.LENGTH_SHORT).show();
                final TextView vod_id = (TextView) view.findViewById(R.id.vod_id);//id

                System.out.println();

//                // 带参跳转
                Intent intent=new Intent();
                intent.putExtra("Activity","movieListActivity");//传一个页面过去
                intent.putExtra("vod_id", vod_id.getText().toString());//传一个id过去
                intent.setClass(movieListActivity.this, moviePalyerActivity.class);//从哪里跳到哪里
                movieListActivity.this.startActivity(intent);


            }
        });


    }


}